package com.hao.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hao.commonutils.JwtUtils;
import com.hao.commonutils.R;
import com.hao.commonutils.ordervo.CourseWebVoOrder;
import com.hao.eduservice.client.OrderClient;
import com.hao.eduservice.entity.EduCourse;
import com.hao.eduservice.entity.chapter.ChapterVo;
import com.hao.eduservice.entity.frontvo.CourseFrontVo;
import com.hao.eduservice.entity.frontvo.CourseWebVo;
import com.hao.eduservice.service.EduChapterService;
import com.hao.eduservice.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 课程前端控制器
 *
 * @author chenWenHao
 * @date 2021/01/06
 */
@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    /**
     * 1 条件查询带分页查询课程
     *
     * @param page          页面
     * @param limit         限制
     * @param courseFrontVo 课程前签证官
     * @return {@link R}
     */
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<EduCourse> pageCourse = new Page<>(page, limit);
        Map<String, Object> map = courseService.getCourseFrontList(pageCourse, courseFrontVo);
        //返回分页所有数据
        return R.ok().data(map);
    }

    /**
     * 2 课程详情的方法
     *
     * @param courseId 进程id
     * @return {@link R}
     */
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);

        //根据课程id查询章节和小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);

        //查询会员是否已经购买课程
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean isBuyCourse = orderClient.isBuyCourse(courseId, memberId);

        return R.ok().data("courseWebVo", courseWebVo).data("chapterVideoList", chapterVideoList).data("isBuy", isBuyCourse);
    }

    /**
     * 获取课程信息,用于订单生成支付模块
     *
     * @param id id
     * @return {@link CourseWebVoOrder}
     */
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id) {
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(id);
        BeanUtils.copyProperties(courseWebVo, courseWebVoOrder);
        return courseWebVoOrder;
    }

}












